<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;

use app\admin\model\Subject;
use app\admin\model\Grade;
use think\Db;

class DashboardController extends CommonController {


    public function suanli() {
        $data = Db::table("ew_dash")->where('type',3)
            ->group('date')->order('date desc')
            ->limit(0,36)->select();
        $duo = count($data);
        if($duo >= 36){
            $del = $data[35]['date'];
            Db::table("ew_dash")->where('type',3)
                ->where('date','<',$del)
                ->delete();
        }
		$data = Db::table("ew_dash")->where('type',3)
            ->group('date')
            ->limit(0,36)->select();
        $date = array();
        $value = array();
        $pjvalue = array();
        foreach ($data as $v){
            $date[] = $v['datetime'];
            $value[] = $v['value'];
            $pjvalue[] = $v['pjvalue'];
        }
        $res = array();
        $res['date'] = $date;
        $res['value'] = $value;
        $res['pjvalue'] = $pjvalue;
        return json_encode($res);
    }
    public function number() {
        $data = Db::table("ew_dash")->where('type',1)
            ->group('date')->order('date desc')
            ->limit(0,36)->select();
        $duo = count($data);
        if($duo >= 36){
            $del = $data[35]['date'];
            Db::table("ew_dash")->where('type',1)
                ->where('date','<',$del)
                ->delete();
        }
		$data = Db::table("ew_dash")->where('type',1)
            ->group('date')
            ->limit(0,36)->select();
        $date = array();
        $value = array();
        foreach ($data as $v){
            $date[] = $v['date'];
            $value[] = $v['value'];
        }
        $res = array();
        $res['date'] = $date;
        $res['value'] = $value;
        return json_encode($res);
    }
    public function price() {
        $data = Db::table("ew_dash")->where('type',2)
            ->group('date')->order('date desc')
            ->limit(0,36)->select();
        $duo = count($data);
        if($duo >= 36){
            $del = $data[35]['date'];
            Db::table("ew_dash")->where('type',2)
                ->where('date','<',$del)
                ->delete();
        }
		$data = Db::table("ew_dash")->where('type',2)
            ->group('date')
            ->limit(0,36)->select();
        $date = array();
        $value = array();
        foreach ($data as $v){
            $date[] = $v['date'];
            $value[] = $v['value'];
        }
        $res = array();
        $res['date'] = $date;
        $res['value'] = $value;
        return json_encode($res);
    }

    public function teacher() {
        $data = Db::table("ew_appuser")->alias('a')
            ->join('ew_subject s','a.majorid=s.s_id','LEFT')
            ->where("typeid",1)->field('s_name as name,count(majorid) as value')->group("majorid")->select();
        return json_encode($data);
    }
    public function sucai() {
        $data = Db::table("ew_sucai")
            ->where("su_special",0)->field('su_type as name,count(su_type) as value')->group("su_type")->select();
        foreach($data as $k=>$v){
            if($v['name']==1){$data[$k]['name'] = "文本";}
            if($v['name']==2){$data[$k]['name'] = "图片";}
            if($v['name']==3){$data[$k]['name'] = "文件";}
            if($v['name']==4){$data[$k]['name'] = "音频";}
            if($v['name']==5){$data[$k]['name'] = "视频";}
        }
        return json_encode($data);
    }
    public function youxiu() {
        $data = Db::table("ew_sucai")
            ->where("su_special",1)->field('su_type as name,count(su_type) as value')->group("su_type")->select();
        foreach($data as $k=>$v){
            if($v['name']==1){$data[$k]['name'] = "文本";}
            if($v['name']==2){$data[$k]['name'] = "图片";}
            if($v['name']==3){$data[$k]['name'] = "文件";}
            if($v['name']==4){$data[$k]['name'] = "音频";}
            if($v['name']==5){$data[$k]['name'] = "视频";}
        }
        return json_encode($data);
    }
    public function course() {
        $data = Db::table("ew_course")->alias('c')
            ->join('ew_subject s','c.c_subjectid=s.s_id','LEFT')
            ->where("c_status",2)->field('s_name as name,count(c_subjectid) as value')->group("c_subjectid")->select();
        return json_encode($data);
    }
    public function project() {
        $data = Db::table("ew_project")->alias('p')
            ->join('ew_project_type pt','p.p_type=pt.pt_id','LEFT')
            ->where("p_status",3)->field('pt_name as name,count(p_type) as value')->group("p_type")->select();
        return json_encode($data);
    }

                                                                           


   //课程统计学生数
    public function getStudentByCourse()
    {	
		if($this->redis()){
            if($this->redis->EXISTS('DgetStudentByCourse'))
                return $this->redis->get("DgetStudentByCourse");
        }
        $campusid = session("loginSession")['campusid'];
        $datas = Db::table('ew_student')
            ->join('ew_course','ew_student.student_courseid=ew_course.id')
            ->where('ew_student.campusid',$campusid)
            ->field('ew_course.course_name as name,count(ew_student.student_courseid) as value')
            ->group('ew_student.student_courseid')
            ->select();
		if($this->redis()) {
            $this->redis->set("DgetStudentByCourse", json_encode($datas));
            $this->redis->expire('DgetStudentByCourse', 7200);
        }
        return json_encode($datas);
    }
    
    /**
     * 
     * 分页填充数据
     */
    public function notice()
    {
         if (input("page") == NULL) {
            $page = 1;
        } else {
            $page = input("page");
        }
        if($page < 1){
            $page =1;
        }
        $rows = 5;
        $campusid = session("loginSession")['campusid'];
        $count = M("notice")->where(["campusid" => $campusid, "type" => '1', "status" => '1'])->count();
        //总页数
        $pagecount = ceil($count/$rows);
        if($page>$pagecount){
            $page = $pagecount;
        }
        
        $data = M("notice")->where(["campusid" => $campusid, "type" => '1', "status" => '1'])->order("time desc")->limit($rows * ($page - 1), $rows)->select();
        
        $this->assign("count", $count);

       
        //dump($data);
        $this->assign("page", $page);
        $this->assign("datas", $data);
        return $this->fetch('notice/watch');
    }
}

